﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using LiveWebServiceBase;
using LiveWebServiceBase.Security;

namespace LiveWebServiceAdmin
{
    public partial class users : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            lbError.Visible = false;
            lbError.Text = "";

            string userName = this.User.Identity.Name;
            bool IsUser = Settings.UserInfo.IsServiceUser(userName);
            bool IsAdmin = Settings.UserInfo.IsAdminUser(userName);

            if (IsUser)
            {
                Response.Redirect("Organizations.aspx?manageuser=" + userName);
            }
            else if (IsAdmin)
            {
                List<string> users = Settings.UserInfo.GetGroupUsers(Settings.UserInfo.userGroupName);

                foreach (string user in users)
                {
                    string _user = user.Split('\\')[1];
                    DirectoryInfo dirinfo = new DirectoryInfo(Settings.Paths.PathToConfigDir + "\\" + _user);

                    if (dirinfo.Exists)
                    {
                        lbServiceUsers.Items.Add(_user);
                    }
                    else
                    {
                        lbServiceUsers.Items.Add(_user + " (user without config directory)");
                    }
                }
            }
        }

        public void UserChanged(object sender, EventArgs arg)
        {
        }

        protected void btnDeleteOrg_Click(object sender, EventArgs e)
        {
            string username = string.Empty;
            string domain = string.Empty;
            string password = string.Empty;

            if (Session["userData"] != null)
            {
                string[] tmp = Authorization.DecryptString(Session["userData"].ToString()).Split(';');

                username = tmp[0];
                domain = (string.IsNullOrEmpty(tmp[1])) ? Environment.MachineName : tmp[1];
                password = tmp[2];
            }

            Impersonator imp = new Impersonator(username, domain, password);

            try
            {
                string editedusername = lbServiceUsers.SelectedItem.Text;

                DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
                DirectoryEntry User = AD.Children.Find(editedusername, "user");

                AD.Children.Remove(User);

                DirectoryInfo dirinfo = new DirectoryInfo(Settings.Paths.PathToConfigDir + "\\" + editedusername);
                dirinfo.Delete(true);

                Page.Response.Redirect(Page.Request.RawUrl);
            }
            catch (Exception ex)
            {
                RequiredFieldValidator validator = new RequiredFieldValidator();
                lbError.ApplyStyle(validator.ControlStyle);
                lbError.Visible = true;
                lbError.Text = ex.Message;
            }
            finally
            {
                imp.Dispose();
            }
        }
    }
}
